package servlet.servlet_yaobo;

import net.sf.json.JSONObject;
import pojo.User;
import service.impl.ImpShoppingMemberService;
import util.ExcelUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 数据的导入
 */
@WebServlet(value = "/imporse")
public class ImporServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String path="C:\\Users\\Yao bo\\Downloads"+req.getParameter("file");
        List<String> list= ExcelUtil.readByFilePath(path);
        if(list==null){
            JSONObject json=new JSONObject();
            json.put("data","导入失败！");
            PrintWriter out=resp.getWriter();
            out.println(json);
            return;
        }
        int bo=0;
        ImpShoppingMemberService imp=new ImpShoppingMemberService();
        for(int i=1;i<list.size();i++){
            User us=new User();
            String[] admin=list.get(i).split("'");
            us.setuID(Integer.parseInt(admin[0]));
            us.setuAddress(admin[1]);
            us.setuPassword(admin[2]);
            us.setuNickName(admin[3]);
            us.setuGender(admin[4].equals("男")?0:1);
            us.setuPhoneNumber(admin[5]);
            us.setuEmail(admin[6]);
            us.setuAddress(admin[7]);
            us.setuIntegral(Integer.parseInt(admin[8]));
            bo=imp.deposit(us);
            if(bo>0){
                bo++;
            }
        }
        JSONObject json=new JSONObject();
        if(bo==0){
            json.put("msg","导入失败");
        }else {
            json.put("msg","导入成功");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
